package com.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.domain.Menber;
import com.user.mapper.MenberMapper;
import com.user.service.MenberService;
import com.user.service.RestaurantService;
import com.user.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author YU
 * @description 针对表【menber】的数据库操作Service实现
 * @createDate 2024-04-15 11:36:11
 */
@Service
public class MenberServiceImpl extends ServiceImpl<MenberMapper, Menber>
        implements MenberService {

    @Resource
    MenberMapper menberMapper;
    @Resource
    RestaurantService restaurantService;

    @Override
    public R addMenber(Menber menber) {
        boolean idExist = restaurantService.isRestaurantIdExist(menber.getRestaurantId());
        if (!idExist) return R.error("餐厅不存在");
        if (menber.getId() == null) {
            //判断当前餐厅的会员等级是否已经存在
            boolean isExist = menberMapper.isMenberExist(menber.getRestaurantId(), menber.getMember());
            if (isExist) return R.error("会员等级已存在");
            int insert = menberMapper.insert(menber);
            if (insert == 1) return R.ok("添加成功");
            else return R.error("添加失败");
        } else {
            //判断当前餐厅的会员等级是否已经存在
            boolean isExist = menberMapper.isMenberExistNo(menber.getRestaurantId(), menber.getMember(), menber.getId());
            if (isExist) return R.error("会员等级已存在");
            int update = menberMapper.updateById(menber);
            if (update == 1) return R.ok("修改成功");
            else return R.error("修改失败");
        }

    }

    @Override
    public R deleteMenber(Integer id) {
        int delete = menberMapper.deleteById(id);
        if (delete == 1) return R.ok("删除成功");
        else return R.error("删除失败");
    }
}




